﻿// -----------------------------------------------------------------------
// <copyright file="FactStudentCourseRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class FactStudentCourseRepository : AbstractRepository<FactStudentCourse>
    {
        public FactStudentCourseRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<FactStudentCourse> GetAll()
        {
            var result = (from x in base._context.FactStudentCourses
                          select new FactStudentCourse
                          {
                              Id = x.Id,
                              CourseCode = x.CourseCode,
                              StudentCode = x.StudentCode,
                              DateRegistered = x.DateRegistered,
                              CourseProgressId = x.CourseProgressId,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(FactStudentCourse entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCourse entityToAdd = new Context.FactStudentCourse
            {
                CourseCode = entity.CourseCode,
                StudentCode = entity.StudentCode,
                DateRegistered = entity.DateRegistered,
                CourseProgressId = entity.CourseProgressId,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.FactStudentCourses.InsertOnSubmit(entityToAdd);
        }

        public override void Update(FactStudentCourse entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCourse entityToUpdate = base._context.FactStudentCourses.Single(x => x.StudentCode.Equals(entity.StudentCode));

            entityToUpdate.CourseCode = entity.CourseCode;
            entityToUpdate.StudentCode = entity.StudentCode;
            entityToUpdate.DateRegistered = entity.DateRegistered;
            entityToUpdate.CourseProgressId = entity.CourseProgressId;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(FactStudentCourse entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.FactStudentCourse entityToDelete = base._context.FactStudentCourses.Single(x => x.StudentCode.Equals(entity.StudentCode));

            base._context.FactStudentCourses.DeleteOnSubmit(entityToDelete);
        }
    }
}
